<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>基本信息</span>
    </div>
    <table class="cs-detail-info">
      <tr>
        <td width="110">ID：</td>
        <td>{{ detail.id }}</td>
        <td width="120">
          手机号：
        </td>
        <td>
          {{ detail.phone }}
          <el-button type="primary" size="mini" @click="editUserPhone(detail)">修改手机</el-button>
        </td>
        <td width="150">最后登录时间：</td>
        <td>{{ detail.loginLastTime }}</td>
      </tr>
      <tr>
        <td>
          昵称：
        </td>
        <td>
          {{ detail.nickname }}
          <el-button type="primary" size="mini" @click="editUserNickname(detail)">修改昵称</el-button>
        </td>
        <td>微信绑定时间：</td>
        <td>{{ detail.weiXinBoundTime | parseTime }}</td>
        <td>创建时间：</td>
        <td>{{ detail.createTime }}</td>
      </tr>
      <tr>
        <td>性别：</td>
        <td>{{ detail.gender | gender }}</td>
        <td>QQ绑定时间：</td>
        <td>{{ detail.qqBoundTime | parseTime }}</td>
        <td>创建设备：</td>
        <td>{{ detail.createMachine }}</td>
      </tr>
      <tr>
        <td>钻石数量：</td>
        <td>{{ detail.diamonds }}</td>
        <td>
          师傅ID：
        </td>
        <td>
          {{ detail.masterId }}
          <el-button type="primary" size="mini" @click="editUserMaster(detail)">修改师傅</el-button>
        </td>
        <td>渠道：</td>
        <td>{{ detail.channelName }}</td>
      </tr>
      <tr>
        <td>积分数量：</td>
        <td>{{ detail.coins / 100 }}</td>
        <td>贡献分成：</td>
        <td>{{ detail.divideCoins / 100 }}</td>
        <td>IP：{{ detail.ipAddress }}</td>
      </tr>
      <tr>
        <td>
          视频卡数量：
        </td>
        <td>{{ detail.chatCards }} ；(总获得数量：{{ extra.cardTotal }})</td>
        <td>徒弟数量：</td>
        <td>{{ detail.disciples }}</td>
        <td>家族ID：</td>
        <td v-if="list.length > 0 && list[list.length - 1].familyId">{{ list[list.length - 1].familyId }}</td>
      </tr>
      <tr>
        <td>语音卡数量：</td>
        <td>{{ detail.chatVoiceCards }}</td>
        <td>VIP:</td>
        <td>{{ detail.vipType === -1 ? '无' : (detail.vipType === 0 ? '白银' : '黄金') }}</td>
        <td>家族名称：</td>
        <td v-if="list.length > 0 && list[list.length - 1].familyName">{{ list[list.length - 1].familyName }}</td>
      </tr>
      <tr>
        <td>
          私聊券数量：
        </td>
        <td>{{ detail.chatTicketsNum }} ；(总获得数量：{{ extra.ticketTotal }})</td>
        <td>VIP到期时间:</td>
        <td>{{ detail.vipTypeLastDate | parseTime }}</td>
      </tr>
      <tr>
        <td>真人认证：</td>
        <td>{{ certificate(detail.liveCertification) }}
          <el-button
            v-if="detail.liveCertification"
            type="danger"
            size="mini"
            @click="certificationHandle(userId, 'live')"
          >
            删除真人认证</el-button>
        </td>

        <td>
          女神认证：
        </td>
        <td>
          {{ certificate(detail.goddessCertification) }}
          <el-button
            v-if="detail.goddessCertification"
            type="danger"
            size="mini"
            @click="certificationHandle(userId, 'anchor')"
          >
            删除女神认证</el-button>
          <el-button
            v-else
            type="danger"
            size="mini"
            @click="certificationHandle(userId, 'confirm')"
          >
            认证女神</el-button>
        </td>
        <td>
          用户版本：
        </td>
        <td>
          {{ detail.version }}
        </td>
      </tr>
      <tr>
        <td>
          主播分成：
        </td>
        <td>
          {{ detail.coinRate || '-' }}
          <el-button type="primary" size="mini" @click="editShareRate(detail)">修改分成</el-button>
        </td>
        <td>
          主播权重：
        </td>
        <td>
          {{ detail.sortWeight || '-' }}
        </td>
        <td>
          账号状态：
        </td>
        <td>
          {{
          detail.bannedType | forbidState
          }}
          <el-button type="danger" size="mini" v-if="detail.bannedType !==0 && detail.bannedType !==1" @click="forbidUser(detail)">封号</el-button>
          <el-button v-if="detail.bannedType===0" type="danger" size="mini" @click="onUnForbid(detail)">解封</el-button>
          <el-button v-if="detail.bannedType===1" type="danger" size="mini" @click="onUnForbid(detail)">解禁</el-button>
        </td>
      </tr>
      <tr>
        <td>
          所属公会ID：
        </td>
        <td>
          {{ detail.teamId }}
        </td>
        <td>
          所属公会名称：
        </td>
        <td>
          {{ detail.teamName }}
        </td>
        <td>
          推荐权重：
        </td>
        <td>
          {{ detail.recommendWeight }}
          <el-button type="primary" size="mini" @click="editRecommendWeight(detail)">修改推荐权重</el-button>
          <el-button v-if="!list[list.length - 1].banedStatus" type="danger" size="mini" @click="forbidUser(detail)">封号</el-button>
        </td>
      </tr>
      <tr>
        <td>
          随机权重n1：
        </td>
        <td>
          {{ detail.randomMatchWeightOne }}
        </td>
        <td>
          随机权重n2：
        </td>
        <td>
          {{ detail.randomMatchWeightTwo }}
          <el-button type="primary" size="mini" @click="editRrandomWeight(detail)">修改随机权重n2</el-button>
        </td>
      </tr>
    </table>
    <DialogForbidAccount
      :visible.sync="dialogVisible"
      :user-ids="forbidAccounts"
      :forbid-type="'0'"
      @onSuccess="$emit('refresh')"
    />
    <DailogUnForbidAccount
      :visible.sync="unForbidDialogVisible"
      :user-ids="forbidAccounts"
      :forbid-type="forbidType"
      @onSuccess="$emit('refresh')"
    />
  </el-card>
</template>
<script>
import { xhrGetCardTicket } from '@/api/account'
import { xhrCancelAnchor, xhrCancelLive, xhrConfirmAnchor, xhrEditPhone, xhrEditNickname, xhrEditMasterId,
  xhrUpdateRandomMatchWeightTwo, xhrChangeWeight, xhrUpdateShareRate } from '@/api/account';
import DialogForbidAccount from '../../components/DialogForbidAccount'
import DailogUnForbidAccount from '../../components/DialogUnForbidAccount'

export default {
  filters: {
    forbidState(val) {
      const obj = {
        0: '封号',
        1: '禁聊'
      }
      return obj[val] ? obj[val] : '正常'
    }
  },
  components: { DialogForbidAccount, DailogUnForbidAccount },
  props: {
    list: {
      type: Array,
      default: null
    },
    detail: {
      type: Object,
      default: null
    }
  },
  data() {
    const query = this.$route.query
    return {
      userId: (query && query.id) || null,
      extra: {},
      forbidAccounts: null,
      forbidType: '0',
      unForbidDialogVisible: false,
      dialogVisible: false
    }
  },
  created() {
    this.doSearch()
  },
  methods: {
    doSearch() {
      xhrGetCardTicket({
        userId: this.userId
      })
        .then(resp => {
          this.extra = resp.data
        })
        .catch(err => {
          this.$message.error(err)
        })
    },
    certificate(val) {
      if (!val) {
        return '未认证'
      }
      if (val === true) {
        return '认证通过'
      } else {
        return '未通过'
      }
    },
    editUserNickname({ id: userId, nickname }) {
        const inputValue = (userId * 100000).toString(32).slice(0, 7).toLocaleUpperCase()
        this.$prompt(`原昵称：${nickname}`, '修改昵称', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: `用户${inputValue}`,
          inputPlaceholder: '请输入新昵称!'
        }).then(({ value }) => {
          xhrEditNickname({
            oldNickName: nickname,
            nickName: value,
            userId
          }).then(() => {
            this.$message({
              type: 'success',
              message: '修改成功'
            });
            this.$emit('refresh')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        });
    },
    editUserPhone({ id: userId, phone }) {
        this.$prompt(`原手机号：${phone}`, '修改手机号', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder: '请输入新手机号!',
          inputPattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
          inputErrorMessage: '手机号格式不正确'
        }).then(({ value }) => {
          xhrEditPhone({
            oldPhone: phone,
            phone: value,
            userId
          }).then(() => {
            this.$message({
              type: 'success',
              message: '修改成功'
            });
            this.$emit('refresh')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        });
    },
    editShareRate({ id: userId, coinRate }) {
        this.$prompt(`原分成：${coinRate}`, '修改分成', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder: '请输入新分成'
        }).then(({ value }) => {
          xhrUpdateShareRate({
            rate: value,
            userId: userId
          }).then(() => {
            this.$message({
              type: 'success',
              message: '修改成功'
            });
            this.$emit('refresh')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        });
    },
    editRecommendWeight({ id: userId, recommendWeight }) {
        this.$prompt(`原推荐权重：${recommendWeight}`, '修改推荐权重', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder: '请输入新权重'
        }).then(({ value }) => {
          xhrChangeWeight({
            weigh: value,
            userId: userId
          }).then(() => {
            this.$message({
              type: 'success',
              message: '修改成功'
            });
            this.$emit('refresh')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        });
    },
    editRrandomWeight(detail){
      this.$prompt("请输入随机权重", '修改随机权重', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        xhrUpdateRandomMatchWeightTwo({
          userId: detail.id,
          weight: value
        }).then(() => {
          this.$message({
            type: 'success',
            message: '修改成功'
          });
          this.$emit('refresh')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      });
    },
    editUserMaster({ id: userId, masterId }) {
        this.$prompt(`原师傅ID：${masterId || '无'}`, '修改师傅', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder: '请输入ID!',
          inputPattern: /\s*|[1|4|6][0-9]{5}$/,
          inputErrorMessage: 'ID格式不正确'
        }).then(({ value }) => {
          xhrEditMasterId({
            oldInviteUserId: masterId || null,
            inviteUserId: value,
            userId
          }).then(() => {
            this.$message({
              type: 'success',
              message: '修改成功'
            });
            this.$emit('refresh')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        });
    },
    forbidUser({ id }) {
      this.forbidAccounts = id + ''
      this.dialogVisible = true
    },
    onUnForbid(row) {
      this.forbidType = row.bannedType === 0 ? '1' : '3'
      this.unForbidDialogVisible = true
      this.forbidAccounts = row.id
    },
    certificationHandle(userId, type) {
      switch (type) {
        case 'live':
          this.$confirm('确定取消真人认证？').then(() => {
            xhrCancelLive({ 'userId': userId })
              .then((resp) => {
                this.$message.success('操作成功')
                location.reload()
              })
              .catch(err => {
                this.$message.error(err)
              })
          })
          break;
        case 'anchor':
          this.$prompt('请输入取消理由', '确定取消女神认证？', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
          }).then(({ value }) => {
            xhrCancelAnchor({ 'userId': userId, 'reason': value })
              .then((resp) => {
                this.$message.success('操作成功')
                location.reload()
              })
              .catch(err => {
                this.$message.error(err)
              })
          }).catch((err) => {
            this.$message.error(err)
          });
          break;
        case 'confirm':
          this.$confirm('确定认证女神？').then(() => {
            xhrConfirmAnchor({ 'userId': userId })
              .then((resp) => {
                this.$message.success('操作成功')
                location.reload()
              })
              .catch(err => {
                this.$message.error(err)
              })
          })
          break;
      }
    }
  }
}
</script>

